package ai2;

import java.util.ArrayList;

public class WorkingMemory {

	// a list containing all the discovered facts
	private ArrayList<String> facts = new ArrayList<String>();
	
	// create a blank WorkingMemory
	public WorkingMemory() {
		System.out.println("WM ** created");
	}
	
	// creating a WorkingMemory from a list of facts
	public WorkingMemory(ArrayList<String> facts) {
		System.out.println("WM ** created with list of facts");
		this.facts = facts;
	}
	
	// return all the facts as a list
	public ArrayList<String> getFacts() {
		System.out.println("WM -- returning facts");
		return facts;
	}
	// set the facts from a list
	public void setFacts(ArrayList<String> facts) {
		System.out.println("WM -- setting facts");
		this.facts = facts;
	}
	// clear all the facts
	public void clearFacts() {
		System.out.println("WM -- clearing facts");
		facts.clear();
	}
	// add a fact to the list of facts
	public void addFact(String fact) {
		// if the fact ends in a period, remove it
		if (fact.substring(fact.length() - 1, fact.length()).equals(".")) {
			fact = fact.substring(0, fact.length() - 1);
		}
		System.out.println("WM -- adding fact: " + fact);
		if (facts.contains(fact)) {
			System.out.println("WM -- fact already existed");
		}
		else {
			facts.add(fact);
		}
	}
	// remove a fact from the list of facts
	public boolean removeFact(String fact) {
		System.out.println("WM -- removing fact: " + fact);
		if (facts.contains(fact)) {
			facts.remove(fact);
			return true;
		}
		else {
			System.out.println("WM -- fact not found");
			return false;
		}
	}
	// find out of a fact is in the list
	public boolean containsFact(String fact) {
		System.out.println("WM -- searching for fact: " + fact);
		/* fix containsFact, doesn't strip the first token off yet */
		return facts.contains(fact);
	}
	// find a matching fact from an antecedent
	public int findMatchingFact(String antecedent) {
		String trimmedAntecedent = antecedent.split("\\?x")[1].trim();
		System.out.println("WM -- trimmedAntecedent: " + trimmedAntecedent);
		int matchingFactIndex = -1;
		for (int i = 0; i < facts.size(); i++) {
			String currentFact = facts.get(i);
			int firstSpace = currentFact.indexOf(" ");
			// remove the name of the object from the fact
			String trimmedFact = currentFact.substring(firstSpace + 1, currentFact.length());
			System.out.println("WM -- trimmedFact: " + trimmedFact);
			System.out.println("WM -- trimmedFact: " + trimmedFact + " vs. trimmedAntecedent: " + trimmedAntecedent);
			if (trimmedFact.equals(trimmedAntecedent)) {
				matchingFactIndex = i;
			}
		}
		System.out.println("WM -- returning matchingFactIndex of: " + matchingFactIndex);
		return matchingFactIndex;
	}
	// retrieve a fact by its index
	public String getFact(int index) {
		return facts.get(index);
	}
	// print all facts
	public void printFacts() {
		System.out.println("WM -- printing facts");
		for (int i = 0; i < facts.size(); i++) {
			int factNumber = i + 1;
			System.out.println("WM -- fact #" + factNumber);
			System.out.println("WM -- " + facts.get(i));
		}
		System.out.println();
	}
}